import 'package:flutter/material.dart';

class StateFulCase extends StatefulWidget {
  const StateFulCase({Key? key, required this.title}) : super(key: key);
  final String title;

  @override
  _StateFulCaseState createState() => _StateFulCaseState();
}

int count = 0;

class _StateFulCaseState extends State<StateFulCase> {
  @override
  Widget build(BuildContext context) {
    return Container(
        child: Column(
      children: [
        Text(
          widget.title,
          style: const TextStyle(fontSize: 20, color: Colors.blue),
        ),
        Row(
          children: [
            ElevatedButton(
                onPressed: () {
                  // 必须把更新数据放在setState的回调函数中
                  setState(() {
                    count--;
                  });
                },
                child: const Text("-")),
            Text(
              count.toString(),
              style: const TextStyle(fontSize: 40, color: Colors.blue),
            ),
            ElevatedButton(
              onPressed: () {
                setState(() {
                  count++;
                });
              },
              child: const Text("+"),
            ),
          ],
        ),
      ],
    ));
  }
}
